﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using log4net;
using AutoFacUtils;
using CashFlow.Db.Entity;
using CashFlow.Web.Service;
using CashFlow.Db.Repository;
using CashFlow.Web.Models;
using CashFlow.Web.MyActionResult;
using CashFlow.Web.Validator;
using CashFlow.Web.Filters;
using CashFlow.Web.Common;

namespace CashFlow.Web.Controllers
{
    public class UserAccountController : Controller
    {
        private static ILog _logger = LogManager.GetLogger(typeof(UserAccountController));
        private IUserAccountInfoService GetUserAccountInfoService() { return AutoFacHelper.Get<IUserAccountInfoService>(); }
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private IUserAccountInfoRepository GetUserAccountInfoRepository() { return AutoFacHelper.Get<IUserAccountInfoRepository>(); }

        public ActionResult UpdateAccountInfo()
        {
            var service = GetUserService();
            var ui = service.GetLoginUser();
            var repos = GetUserAccountInfoRepository();
            var entity = repos.QuerySingle(x => x.UserId == ui.Id);
            ViewBag.UserAccountInfo = entity;

            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateUserAccountInfoModelValidator))]
        public ActionResult UpdateAccountInfo(UpdateUserAccountInfoModel model)
        {
            var service = GetUserAccountInfoService();
            bool bSuccess = service.UpdateAccountInfo(
                                model.BankAccountName
                                , model.BankName
                                , model.BankCardNo
                                , model.WechatPath
                                , model.AlipayPath
                            );
            return MyCommonTool.CreateOperationResult(bSuccess, "", null);
        }

        public ActionResult UpdateAddress()
        {
            var service = GetUserService();
            var ui = service.GetLoginUser();
            var repos = GetUserAccountInfoRepository();
            var info = repos.QuerySingle(x => x.UserId == ui.Id);
            ViewBag.Address = info != null ? info.Address : "";

            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateAddressModelValidator))]
        public ActionResult UpdateAddress(UpdateAddressModel model)
        {
            var service = GetUserAccountInfoService();
            bool bSuccess = service.UpdateAddress(model.Address);
            return MyCommonTool.CreateOperationResult(bSuccess, "", null);
        }

        [HttpPost]
        public ActionResult UploadImage(HttpPostedFileBase file)
        {
            bool bSuccess = false;
            string msg = "";
            string Path = "";
            if (file != null)
            {
                if (file.ContentLength > 0)
                {
                    string extensionName = System.IO.Path.GetExtension(file.FileName);
                    string newFileName = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss_fff") + extensionName;
                    string newPath = "/UploadFile/" + newFileName;
                    Path = newPath;
                    string path = Server.MapPath(newPath);
                    string dir = Server.MapPath("~/UploadFile/");
                    if (!System.IO.Directory.Exists(dir))
                    {
                        System.IO.Directory.CreateDirectory(dir);
                    }
                    file.SaveAs(path);
                    bSuccess = true;
                }
                else
                {
                    msg = "不能上传空文件";
                }
            }
            else
            {
                msg = "不能上传空文件";
            }

            return MyCommonTool.CreateOperationResult(bSuccess, msg, Path);
        }
    }
}